package com.qingxin.park.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.DetectionData;
import com.qingxin.park.vo.CropType;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.StringUtils;

/**
 * 农残检测仪数据
 * @author Administrator
 *
 */
@Service
public class DetectionDataService extends BaseService<DetectionData, String> {
	
	/**
	 * 速测数据列表（针对某一产品）
	 * @param pdtid
	 * @param search
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String pdtid,String search,PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from DetectionData where pdtid=:pdtid");
		querier.setParam("pdtid", pdtid);
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (detectionItem like :detectionItem)");
			querier.setParam("detectionItem", search);
		}
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 列表数据
	 * @param pdtid
	 * @param pageParam
	 * @return
	 */
	public Pager list(String pdtid, PageParam pageParam){
		Querier querier = Querier.create("from DetectionData where 1=1");
		if(StringUtils.isNotEmpty(pdtid)){
			querier.append(" and pdtid=:pdtid ");
			querier.setParam("pdtid", pdtid);
		}
		
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 获取指定产品的监测信息
	 * @param pdtid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<DetectionData> list(String pdtid){
		List<DetectionData> list = listByHql("from DetectionData where pdtid=? order by createTime desc", pdtid);
		return list;
	}
	
	
	/**
	 * 添加
	 * @param detectionData
	 * @param request
	 * @return
	 */
	public SysError add(DetectionData detectionData, HttpServletRequest request){
		detectionData.setCreateTime(new Date());
		this.save(detectionData);
		return null;
	}
	
	
	/**
	 * 删除
	 * @param ids
	 * @param request
	 * @return
	 */
	public SysError delete(String[] ids, HttpServletRequest request){
		for(String id : ids){
			DetectionData data = get(id);
			if(data == null){
				continue;
			}
			this.delete(data);
		}
		return null;
	}
	
	
	/**
	 * 修改状态信息
	 * @param id
	 * @param testStandard
	 * @param testStatus
	 * @param testResult
	 * @return
	 */
	public SysError updateStatus(String id, String testStandard, String testStatus, String testResult) {
		DetectionData data = getSafeEntity(id);
		if(data==null){
			return new SysError("参数错误");
		}
		data.setTestStandard(testStandard);
		data.setTestStatus(testStatus);
		data.setTestResult(testResult);
		
		Updater<DetectionData> updater = new Updater<>(data, "testStandard,testStatus,testResult");
		this.update(updater);
		
		return null;
	}
	
	
	
	/**
	 * 获取作物分类信息
	 * @param type 类型：1种植、2养殖、3加工
	 * @param cropid
	 * @return
	 */
	public CropType getCropType(String type, String cropid){
		if("1".equals(type)){
			CropType cropType = (CropType) getByHql("select new com.qingxin.park.vo.CropType(t.typeid,t.typeName)" +
					" from CropsType t, Crops c where t.typeid=c.cropsType.typeid and c.cropid=?", cropid);
			return cropType;
		}else if("2".equals(type)){
			CropType cropType = (CropType) getByHql("select new com.qingxin.park.vo.CropType(t.typeid,t.typeName)" +
					" from CropsyzType t, Cropsyz c where t.typeid=c.cropsyzType.typeid and c.cropid=?", cropid);
			return cropType;
		}else {
			CropType cropType = (CropType) getByHql("select new com.qingxin.park.vo.CropType(t.typeid,t.typeName)" +
					" from CropsjgType t, Cropsjg c where t.typeid=c.cropsjgType.typeid and c.cropid=?", cropid);
			return cropType;
		}
	}
	
	
	
}
